package com.ihr360.payroll.service.reference.validator;

import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.validation.ValidationUtils;
import com.ihr360.validation.Validator;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

@Component
public class FormularValidator implements Validator<String> {
    @Override
    public boolean supports(Class<?> clazz) {
        return String.class.equals(clazz);
    }

    @Override
    public void validate(String formular) {
        ValidationUtils.rejectIfEmptyOrWhitespace(formular, null, "薪资项目公式不能为空！");
        Pattern pattern = compile("\\s*|\t|\r|\n");
        Matcher matcher = pattern.matcher(formular);
        formular = matcher.replaceAll("");
        if (!formular.startsWith("=")) {
            throw new Ihr360Exception(null, "薪资项目公式必须以'='开头");
        }
    }
}
